package user

import (
	"wx-uniapp-go/internal/domain/errcode"
	"wx-uniapp-go/internal/logic/coupon_logic"
	"wx-uniapp-go/internal/repository/dto"
	"wx-uniapp-go/internal/server/echo"

	"github.com/gin-gonic/gin"
)

// CreateCouponRule 创建优惠券规则
func CreateCouponRule(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.CreateCouponRuleReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := coupon_logic.NewCouponLogic(ctx).CreateCouponRule(req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// UpdateCouponRule 更新优惠券规则
func UpdateCouponRule(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.UpdateCouponRuleReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := coupon_logic.NewCouponLogic(ctx).UpdateCouponRule(req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// DeleteCouponRule 删除优惠券规则
func DeleteCouponRule(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	id := c.GetInt64("id")
	if id <= 0 {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := coupon_logic.NewCouponLogic(ctx).DeleteCouponRule(id); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// ListCouponRules 查询优惠券规则列表
func ListCouponRules(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.ListCouponRuleReq)
	if err := c.ShouldBindQuery(req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	resp, err := coupon_logic.NewCouponLogic(ctx).ListCouponRules(req)
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c, resp)
}
